home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / Interapplication Communication / SmallDaemon / pSmallDaemon.p < prev    next >
Encoding:
Text File  |  1993-01-18  |  3.8 KB  |  138 lines  |  [TEXT/MPS ]

  1. (*
  2.  *  pSmallDaemon
  3.  *
  4.  *  7/92 Greg Robbins         based on code by C.K. Haun
  5.  *
  6.  *  This is a minimal faceless background application for System 7.
  7.  *
  8.  *  It demonstrates how to install and dispatch Apple events, as well
  9.  *  as the other bare essentials for a faceless background app.
  10.  *
  11.  *  The file type for this application should be 'APPL' if it will be launched
  12.  *  like an application or 'appe' if it will be placed into the Extensions
  13.  *  folder and launched at startup.  'appe' files can also have an INIT resource 
  14.  *  to put up an icon (using ShowInit) at startup.
  15.  *)
  16.  
  17.  
  18. PROGRAM pSmallDaemon;
  19.  
  20.     USES AppleEvents, GestaltEqu;
  21.     
  22.     CONST
  23.         kSleepMax = MAXLONGINT; { long sleep time to avoid stealing cycles         }
  24.                                 { an app which does something on null events might }
  25.                                 { sleep less                                       }
  26.     VAR
  27.         gQuitFlag, gAppleEventsFlag: Boolean;
  28.         gSleepVal: LongInt;
  29.         
  30.         gRetCode: OSErr;
  31.         gGestResponse: LongInt;
  32.         gMainEventRec: EventRecord;
  33.         gEventFlag: Boolean;
  34.     
  35.     
  36.     { Apple event handlers to be installed }
  37.  
  38.     FUNCTION DoAEOpenApplication(theAEvent: AppleEvent;
  39.         reply: AppleEvent; refcon: LONGINT): OSErr;
  40.     BEGIN
  41.         DoAEOpenApplication := noErr;
  42.     END;
  43.         
  44.     FUNCTION DoAEOpenDocuments(theAEvent: AppleEvent;
  45.         reply: AppleEvent; refcon: LONGINT): OSErr;
  46.     BEGIN
  47.         DoAEOpenDocuments := errAEEventNotHandled;
  48.     END;
  49.  
  50.     FUNCTION DoAEPrintDocuments(theAEvent: AppleEvent;
  51.         reply: AppleEvent; refcon: LONGINT): OSErr;
  52.     BEGIN
  53.         DoAEPrintDocuments := errAEEventNotHandled;
  54.     END;
  55.         
  56.     FUNCTION DoAEQuitApplication(theAEvent: AppleEvent;
  57.         reply: AppleEvent; refcon: LONGINT): OSErr;
  58.     BEGIN
  59.         gQuitFlag := TRUE;
  60.         DoAEQuitApplication := noErr;
  61.     END; { DoAEQuit }
  62.     
  63.     PROCEDURE InitAppleEventsStuff;
  64.     VAR
  65.         retCode: OSErr;
  66.     BEGIN
  67.         IF gAppleEventsFlag THEN
  68.         BEGIN
  69.             retCode := AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, 
  70.                 @DoAEOpenApplication, 0, FALSE);
  71.             IF retCode = noErr THEN
  72.                 retCode := AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
  73.                     @DoAEOpenDocuments, 0, FALSE);
  74.             IF retCode = noErr THEN
  75.                 retCode := AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, 
  76.                     @DoAEPrintDocuments, 0, FALSE);
  77.             IF retCode = noErr THEN
  78.                 retCode := AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
  79.                     @DoAEQuitApplication, 0, FALSE);
  80.             IF retCode <> noErr THEN
  81.                 DebugStr('Install event handler failed');
  82.             { a better way to indicate an error is to post a notification }
  83.         END;
  84.     END;
  85.     
  86.     PROCEDURE DoHighLevelEvent(theEventRec: EventRecord);
  87.     { high-level event dispatching }
  88.     VAR
  89.         retCode: OSErr;
  90.     BEGIN
  91.         retCode := AEProcessAppleEvent(theEventRec)
  92.     END;
  93.         
  94. BEGIN { main program }
  95.  
  96.     { faceless background apps only get a 2K stack by default.  If necessary,
  97.       increase the stack size here (by calling GetApplLimit to find the current
  98.       heap limit, and SetApplLimit to set it to a lower address, thus reserving
  99.       more space for the stack)                                                 }
  100.     
  101.     { initialize QuickDraw globals }
  102.     
  103.     InitGraf(@thePort);
  104.     
  105.     { initialize application globals }
  106.     
  107.     gQuitFlag := FALSE;
  108.     gSleepVal := kSleepMax;
  109.     
  110.     { is the Apple Event Manager available? }
  111.     
  112.     gRetCode := Gestalt(GestaltAppleEventsAttr, gGestResponse);
  113.     IF (gRetCode = noErr) AND BTST(gGestResponse, gestaltAppleEventsPresent) THEN
  114.         gAppleEventsFlag := TRUE
  115.     ELSE
  116.         gAppleEventsFlag := FALSE;
  117.         
  118.     { install Apple event handlers }
  119.     
  120.     InitAppleEventsStuff;
  121.     
  122.     { main event loop }
  123.     
  124.     WHILE NOT(gQuitFlag) DO
  125.     BEGIN
  126.         gEventFlag := 
  127.             WaitNextEvent(highLevelEventMask, gMainEventRec, gSleepVal, NIL);
  128.  
  129.         { faceless background tasks receive only high-level events }
  130.         IF gMainEventRec.what = kHighLevelEvent THEN
  131.             DoHighLevelEvent(gMainEventRec);
  132.         
  133.         { during testing, I like to call GetKeys here and check if the CapsLock
  134.           key is down.  If it is, set gQuitFlag so the program will exit. }
  135.     END;    
  136.     
  137. END.
  138.